package com.ciban.smarthome.common.utils.logcrash;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

/**
 * 日志记录
 * 
 */
public class LogUtil {
	private static String savePath = Environment.getExternalStorageDirectory()
			.getAbsolutePath()
			+ File.separator
			+ "rrcx"
			+ File.separator
			+ "Log" + File.separator;

	/**
	 * 开发阶段
	 */
	private static final int DEVELOP = 0;
	/**
	 * 内部测试阶段
	 */
	private static final int DEBUG = 1;
	/**
	 * 公开测试
	 */
	private static final int BATE = 2;
	/**
	 * 正式版
	 */
	private static final int RELEASE = 3;

	/**
	 * 当前阶段标示
	 */
	private static int currentStage = DEVELOP;

	public static void info(String msg) {
		i(LogUtil.class, msg);
	}

	public static void i(Class clazz, String msg) {
		switch (currentStage) {
		case DEVELOP:
			Log.i(clazz.getSimpleName(), msg);
			break;
		case DEBUG:
			// 在应用下面创建目录存放日志
			break;
		case BATE:
			// write to sdcard

			break;
		case RELEASE:
			// 一般不做日志记录
			break;
		}
	}

	public static void d(Class clazz, String msg) {
		switch (currentStage) {
		case DEVELOP:
			Log.d(clazz.getSimpleName(), msg);
			break;
		case DEBUG:
			// 在应用下面创建目录存放日志
			break;
		case BATE:
			// write to sdcard

			break;
		case RELEASE:
			// 一般不做日志记录
			break;
		}
	}

	public static void v(Class clazz, String msg) {
		switch (currentStage) {
		case DEVELOP:
			Log.v(clazz.getSimpleName(), msg);
			break;
		case DEBUG:
			// 在应用下面创建目录存放日志
			break;
		case BATE:
			// write to sdcard

			break;
		case RELEASE:
			// 一般不做日志记录
			break;
		}
	}

	public static void e(Class clazz, String msg) {
		switch (currentStage) {
		case DEVELOP:
			Log.e(clazz.getSimpleName(), msg);
			break;
		case DEBUG:
			// 在应用下面创建目录存放日志
			break;
		case BATE:
			// write to sdcard

			break;
		case RELEASE:
			// 一般不做日志记录
			break;
		}
	}

	public static void w(Class clazz, String msg) {
		switch (currentStage) {
		case DEVELOP:
			Log.w(clazz.getSimpleName(), msg);
			break;
		case DEBUG:
			// 在应用下面创建目录存放日志
			break;
		case BATE:
			// write to sdcard

			break;
		case RELEASE:
			// 一般不做日志记录
			break;
		}
	}

	public static void writeLogToSdcard(String log, String name) {
		writeLog(log, name, savePath);
	}

	public static void writeLogCrash(String log) {
		writeLog(log, "crash", savePath);
	}

	/**
	 * 
	 * @param log
	 * @param path
	 * @return 返回写入的文件路径 写入Log信息的方法，写入到SD卡里面
	 */
	public static String writeLog(String log, String name, String path) {
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
		String timestamp = name + format.format(new Date()) + ".log";
		String filename = path + timestamp;
		File file = new File(filename);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		try {
			file.createNewFile();
			FileWriter fw = new FileWriter(file, true);
			BufferedWriter bw = new BufferedWriter(fw);
			// 写入相关Log到文件
			bw.write(log);
			bw.newLine();
			bw.close();
			fw.close();
			return filename;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
